#include "IntroState.h"

#include "PlayState.h"
#include "Client.h"
#include "Server.h"

using ouden::CGameMgr;

template<>
CIntroState* ouden::CSingleton<CIntroState>::m_pInstance = 0;

CIntroState::CIntroState()
{
	//ctor
	
}

CIntroState::~CIntroState()
{
	//dtor
	
}

void CIntroState::onEnter()
{
	
	//Set the exit variable to false
	m_bExit = false;
	
	//Set up the ouden log manager for the whole game
	m_fsLogFile.open("Ouden.log", std::ios::out);
	//ouden::CLogMgr::getInstance()->setLogStream(&m_fsLogFile);
	ouden::CLogMgr::getInstance()->setLogStream(&std::cout);
	ouden::CLogMgr::getInstance()->setLoggingLevel(ouden::LL_DEBUG);
	
	//=========Fade Rectangle Setup=========
	//Create the fade rectangle with texture coordinates
	/*m_pFadeRect = new Ogre::Rectangle2D(true);
	
	//Cover the whole screen
	m_pFadeRect->setCorners(0.0, 0.0, 1.0, 1.0);
	
	//Set the material
	m_pFadeRect->setMaterial("Plastic/Green");
	
	//Render the fade rectangle after everything else
	m_pFadeRect->setRenderQueueGroup(Ogre::RENDER_QUEUE_OVERLAY);
	
	// Attach the fade rectangle to the scene
	Ogre::SceneNode* pFadeRectSceneNode = CGameMgr::getInstance()->getSceneMgr()->getRootSceneNode()->createChildSceneNode("FadeRect");
	pFadeRectSceneNode->attachObject(m_pFadeRect);
	pFadeRectSceneNode->translate(0, 0, -0.5);
	pFadeRectSceneNode->scale(0.1, 0.1, 0.1);*/
	
	/*
	Ogre::Entity* pBoxEntity = CGameMgr::getInstance()->getSceneMgr()->createEntity("box1", "cube.mesh");
	
	pBoxEntity->setMaterialName("Plastic/Green");
	
	Ogre::SceneNode* pBoxNode = CGameMgr::getInstance()->getSceneMgr()->getRootSceneNode()->
				createChildSceneNode("box1");
	
	pBoxNode->translate(0, 0, -0.5);
	pBoxNode->scale(0.1, 0.1, 0.1);
	
	pBoxNode->attachObject(pBoxEntity);
	*/
	
	//RENDER_QUEUE_OVERLAY and 'depth_check off' 
	//======================================
	
	//Set the initial screen index to 0
	m_iCurScreen = 0;
	
	//Add the intro screen overlays to the list
	m_pIntroScreens.push_back(Ogre::OverlayManager::getSingleton().getByName("IntroScreenOverlay/OudenTitle"));
	m_pIntroScreens.push_back(Ogre::OverlayManager::getSingleton().getByName("IntroScreenOverlay/DeveloperSplash"));
	
	//Show the first intro screen
	m_pIntroScreens[0]->show();
	
};

void CIntroState::onExit()
{
	
	//Delete the fade rectangle
	if (m_pFadeRect)
		delete m_pFadeRect;
	
	CGameMgr::getInstance()->getSceneMgr()->clearScene();
	
}

void CIntroState::onKeyDown(ouden::KeyCode iKey)
{
	
	if (Ogre::KC_ESCAPE == iKey) {
		
		m_bExit = true;
		return;
		
	}
	
	//Hide the old intro screen
	m_pIntroScreens[m_iCurScreen]->hide();
	
	if (Ogre::KC_C == iKey) {
		
		CGameMgr::getInstance()->beginClientConnection(CClient::getInstance(), "127.0.0.1", 60002, 60003);
		changeState(CPlayState::getInstance());
		return;
		
	}
	
	if (Ogre::KC_S == iKey) {
		
		CGameMgr::getInstance()->beginServerConnection(CServer::getInstance(), 2, 60002);
		changeState(CPlayState::getInstance());
		return;
		
	}
	
	//Advance to the next screen
	++m_iCurScreen;
	
	if (m_iCurScreen < m_pIntroScreens.size()) {
		
		//Show the new intro screen
		m_pIntroScreens[m_iCurScreen]->show();
		
	}
	else {
		
		//We have reached the end of the intro screens
		
		//Change the game state to the main menu
		//changeState(MenuState::getInstance());
		changeState(CPlayState::getInstance());
		
	}
	
}

bool CIntroState::onTick(ouden::CGameTimer* pTimer)
{
	
	return true;
	
}

bool CIntroState::onFrameStart(ouden::CGameTimer* pTimer)
{
	
	static float f = 1.f;
	
	f -= 0.001f;
	if (f < 0.f) f = 0.f;
	//m_pIntroScreens[m_iCurScreen]->get2DElementsIterator().getNext()->getMaterial()->setLightingEnabled(true); //Diffuse(1, 1, 1, f);
	
	
	// Set the ambient light level.
	//m_pSceneMgr->setAmbientLight(Ogre::ColourValue(1, f, f));
	
	return true;
	
}

bool CIntroState::frameEnded()
{
	
	if (m_bExit)
		return false;
		
	return true;
	
}
